# Contributor: Timo Teräs <timo.teras@iki.fi>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

pkgname=edk2
pkgver=0.0.202308
_realver=edk2-stable${pkgver##*.}
_sslver=3.0.9
_sfver=3e
pkgrel=1
pkgdesc="EFI Development Kit II"
url="https://github.com/tianocore/tianocore.github.io/wiki/EDK-II/"
arch="x86_64 aarch64"
license="BSD-2-Clause-Patent"
makedepends="bash python3 iasl nasm util-linux-dev util-linux-misc"
options="!archcheck !check" # has no checks
subpackages="$pkgname-pyc $pkgname-shell:_shell"
_mipisyst_commit=370b5944c046bab043dd8b133727b2135af7747a
source="$pkgname-$pkgver.tar.gz::https://github.com/tianocore/edk2/archive/$_realver.tar.gz
	mipisyst-$_mipisyst_commit.tar.gz::https://github.com/MIPI-Alliance/public-mipi-sys-t/archive/$_mipisyst_commit.tar.gz
	https://www.openssl.org/source/openssl-$_sslver.tar.gz
	http://www.jhauser.us/arithmetic/SoftFloat-$_sfver.zip
	build-hack.patch
	0008-BaseTools-do-not-build-BrotliCompress-RH-only.patch
	0009-MdeModulePkg-remove-package-private-Brotli-include-p.patch
	"
builddir="$srcdir/$pkgname-$_realver"

PLATFORM="ShellPkg/ShellPkg.dsc"
case "$CARCH" in
	x86)
		TARGET_ARCH=IA32
		PLATFORM="$PLATFORM OvmfPkg/OvmfPkgIa32X64.dsc"
		;;
	x86_64)
		TARGET_ARCH=X64
		PLATFORM="$PLATFORM OvmfPkg/OvmfPkgX64.dsc OvmfPkg/OvmfXen.dsc"
		subpackages="$subpackages ovmf:_ovmf:noarch ovmf-xen:_xen:noarch"
		;;
	aarch64)
		TARGET_ARCH=AARCH64
		PLATFORM="$PLATFORM ArmVirtPkg/ArmVirtQemu.dsc"
		subpackages="$subpackages aavmf::noarch"
		;;
esac

TOOLCHAIN=GCC5
RELEASE=RELEASE

prepare() {
	# unix line endings for the files to be patched
	sed -e 's/\r$//' -i BaseTools/Source/C/VfrCompile/VfrUtilityLib.cpp \
		BaseTools/Source/C/VolInfo/VolInfo.c
	rm -rf CryptoPkg/Library/OpensslLib/openssl
	ln -s "$srcdir"/openssl-$_sslver CryptoPkg/Library/OpensslLib/openssl
	rm -rf ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3
	ln -s "$srcdir"/SoftFloat-$_sfver \
		ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3

	rm -rf MdePkg/Library/MipiSysTLib/mipisyst
	ln -s  "$srcdir"/public-mipi-sys-t-$_mipisyst_commit \
		MdePkg/Library/MipiSysTLib/mipisyst

	default_prepare
}

build() {
	export PYTHON_COMMAND=python3
	export WORKSPACE=$PWD
	export PACKAGES_PATH=$PWD
	export EDK_TOOLS_PATH=$PWD/BaseTools/
	export PATH=$PWD/BaseTools/BinWrappers/PosixLike/:$PATH
	# parallel build fails
	unset MAKEFLAGS

	bash -c ". edksetup.sh"
	make -C BaseTools

	for _p in $PLATFORM; do
		msg "Building Plaform Files $_p"
		command build -b $RELEASE \
			-a $TARGET_ARCH  \
			-t $TOOLCHAIN \
			-p $_p \
			-n ${JOBS:-2} \
			-DSECURE_BOOT_ENABLE=TRUE \
			-DTPM2_ENABLE=TRUE
	done
}

package() {
	mkdir -p "$pkgdir"/usr/bin \
		"$pkgdir"/usr/share/$pkgname/Conf \
		"$pkgdir"/usr/share/$pkgname/Scripts

	install BaseTools/Source/C/bin/* BaseTools/BinWrappers/PosixLike/LzmaF86Compress \
		"$pkgdir"/usr/bin
	install BaseTools/BuildEnv "$pkgdir"/usr/share/$pkgname/
	install BaseTools/Conf/*.template "$pkgdir"/usr/share/$pkgname/Conf
	install BaseTools/Scripts/GccBase.lds "$pkgdir"/usr/share/$pkgname/Scripts

	for i in $(find BaseTools/Source/Python -type d -maxdepth 1); do
		local mod=${i##*/}
		test -f "$i/$mod.py" || continue
		cp -R BaseTools/Source/Python/"$mod" "$pkgdir"/usr/share/edk2/Python/
		cat <<- EOF > "$pkgdir"/usr/bin/"$mod".py
		#!/bin/sh
		export PYTHONPATH=/usr/share/edk2/Python
		exec $PYTHON_COMMAND /usr/share/edk2/Python/$mod/$mod.py "\$@"
		EOF
		chmod +x "$pkgdir"/usr/bin/"$mod".py
	done
}

_shell() {
	pkgdesc="EDK2 UEFI Shell"

	# taken from arch
	# minimal UEFI shell, as defined in ShellPkg/Application/Shell/Shell.inf
	local _min='7C04A583-9E3E-4f1c-AD65-E05268D0B4D1'
	# full UEFI shell, as defined in ShellPkg/ShellPkg.dsc
	local _full='EA4BB293-2D7F-4456-A681-1F22F42CD0BC'

	install -D "$builddir"/Build/Shell/"$RELEASE"_"$TOOLCHAIN"/$TARGET_ARCH/Shell_$_min.efi \
		"$subpkgdir"/usr/share/edk2-shell/Shell.efi

	install -D "$builddir"/Build/Shell/"$RELEASE"_"$TOOLCHAIN"/$TARGET_ARCH/Shell_$_full.efi \
		"$subpkgdir"/usr/share/edk2-shell/ShellFull.efi
}

_ovmf() {
	pkgdesc="Open Virtual Machine Firmware (OVMF) BIOS"
	license="BSD MIT"

	for fw in "$builddir"/Build/OvmfX64/"$RELEASE"_"$TOOLCHAIN"/FV/*.fd; do
		install -D $fw "$subpkgdir"/usr/share/OVMF/${fw##*/}
	done

	# dont ship memfd for now to save space
	rm -f "$subpkgdir"/usr/share/OVMF/MEMFD.fd

	install -d "$subpkgdir"/usr/share/ovmf
	ln -sf ../OVMF/OVMF.fd "$subpkgdir"/usr/share/ovmf/bios.bin
}

_xen() {
	pkgdesc="Open Virtual Machine Firmware (OVMF) - Xen build"
	license="BSD MIT"

	install -D "$builddir"/Build/OvmfXen/"$RELEASE"_"$TOOLCHAIN"/FV/OVMF.fd \
		"$subpkgdir"/usr/lib/xen/boot/ovmf.bin
}

aavmf() {
	pkgdesc="ARM (aarch64) Virtual Machine Firmware EFI"
	license="BSD MIT"

	dd if=/dev/zero \
		of="$builddir"/Build/ArmVirtQemu-AARCH64/"$RELEASE"_$TOOLCHAIN/FV/AAVMF_CODE.fd \
		bs=1M seek=64 count=0
	dd if="$builddir"/Build/ArmVirtQemu-AARCH64/"$RELEASE"_$TOOLCHAIN/FV/QEMU_EFI.fd \
		of="$builddir"/Build/ArmVirtQemu-AARCH64/"$RELEASE"_$TOOLCHAIN/FV/AAVMF_CODE.fd \
		conv=notrunc
	dd if=/dev/zero \
	of="$builddir"/Build/ArmVirtQemu-AARCH64/"$RELEASE"_$TOOLCHAIN/FV/AAVMF_VARS.fd \
		bs=1M seek=64 count=0

	for fw in "$builddir"/Build/*/"$RELEASE"_"$TOOLCHAIN"/FV/*.fd; do
		install -D $fw "$subpkgdir"/usr/share/AAVMF/${fw##*/}
	done
}

pyc() {
	default_pyc

	local IFS=$'\n'
	amove $(find usr/share/edk2/Python -type d -name __pycache__)
}

sha512sums="
668411dc64a4a69afd145221c599fffc3797de26e801dda7d9b7ed92f755ff4fda4635dbc21c821f527e56eb71c4ad98c1fb079112a56d6b6eea5ff4d010e3cf  edk2-0.0.202308.tar.gz
de6888577ceab7ab6915d792f3c48248cfa53357ccd310fc7f7eae4d25a932de8c7c23e5b898c9ebf61cf86cb538277273f2eb131a628b3bf0d46c9a3b9b6686  mipisyst-370b5944c046bab043dd8b133727b2135af7747a.tar.gz
86c99146b37236419b110db77dd3ac3992e6bed78c258f0cc3434ca233460b4e17c0ac81d7058547fe9cb72a9fd80ee56d4b4916bb731dbe2bbcf1c3d46bf31a  openssl-3.0.9.tar.gz
3fedcd0060affb2d8fc7995894133cfed6a495c8717df0d30c89885223c38749f25743598383736036332dad6353c6a3f027f5a94a696660f7c4b607e33e534c  SoftFloat-3e.zip
a7d4ab2c82b62ba01c86e59f53bd3896d661c9bfbb9db9598734155b66d5fe03eca4a2a9993a14d3bf555992c6d01ba5d7a15868ff9ec6ed98b8a9b3895bb7df  build-hack.patch
ecbfc1ec3b732580c33c477191b71553247af1a68f1754bd363d179e0f5aabde93e3c5ec7f2574f9a9ffefef34e75787a2a87b1057b02cd206e8f0618a252871  0008-BaseTools-do-not-build-BrotliCompress-RH-only.patch
ecad98ff84ab307bda751c8a9a321e064ef880dc66b4d107e66aedbc4e14d00eed76770437e25fa9153dc30803f5cbbf1299329f56865a3b75d2c19f6615e68b  0009-MdeModulePkg-remove-package-private-Brotli-include-p.patch
"
